package com.luoxiang.socialandroid.dialog

import android.content.Context
import android.widget.Toast
import com.blankj.utilcode.util.LogUtils
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.luoxiang.lib_common.common.Constants
import com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA
import com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.adapter.OneTouchChatAdapter
import com.luoxiang.socialandroid.bean.CallContent
import com.luoxiang.socialandroid.bean.NumberBean
import com.luoxiang.socialandroid.bean.SayHelloBean
import com.luoxiang.socialandroid.databinding.DialogOneTouchChatBinding
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.MmkvUtil
import com.luoxiang.socialandroid.utils.NetUtils
import com.lxj.xpopup.core.BottomPopupView
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class OneTouchChatDialog(context: Context) : BottomPopupView(context) {
    companion object {
        val data: ArrayList<CallContent> = arrayListOf()
    }

    private lateinit var mViewBinding: DialogOneTouchChatBinding
    private lateinit var adapter: OneTouchChatAdapter

    override fun getImplLayoutId() = R.layout.dialog_one_touch_chat

    override fun onCreate() {
        super.onCreate()
        mViewBinding = DialogOneTouchChatBinding.bind(popupImplView)
        //设置布局管理器
        val flexboxLayoutManager = FlexboxLayoutManager(context)
        flexboxLayoutManager.flexDirection = FlexDirection.ROW
        flexboxLayoutManager.flexWrap = FlexWrap.WRAP
        flexboxLayoutManager.justifyContent = JustifyContent.FLEX_START
        mViewBinding.recyclerView.layoutManager = flexboxLayoutManager
        adapter = OneTouchChatAdapter(data)
        mViewBinding.recyclerView.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            adapter.selectIndex = position
            adapter.notifyDataSetChanged()
        }
        mViewBinding.imageClose.setOnClickListener {
            dismiss()
        }
        mViewBinding.imageOk.setOnClickListener {
            if (data.size <= adapter.selectIndex) return@setOnClickListener
            sayHelloAccost(data[adapter.selectIndex].id.toString())
            dismiss()
        }
    }

    private fun sayHelloAccost(contentId: String) {
        NetUtils.getInstance().apiService.sayHelloAccost(
            EXTRA,
            MmkvUtil.decodeString(TOKEN),
            contentId
        ).subscribeOn(
            Schedulers.io()
        )
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NumberBean> {
                override fun onSubscribe(d: Disposable?) {}

                override fun onError(e: Throwable?) {}

                override fun onComplete() {}

                override fun onNext(value: NumberBean?) {
                    val data = value?.data
                    var decryptStr: String? = null
                    try {
                        decryptStr = AESUtil.aesDecryptString(data, Constants.MmkvConst.KEY)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    LogUtils.i("decryptStr10", decryptStr)
                    val bean = Gson().fromJson<SayHelloBean>(
                        decryptStr,
                        object : TypeToken<SayHelloBean>() {}.type
                    )
                    if (bean?.data?.sayCode == 1) {
                        Toast.makeText(context, "搭讪成功", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(context, "搭讪频繁", Toast.LENGTH_SHORT).show()
                    }
                }
            })
    }
}